package web.directories.action;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import domain.entities.*;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.ResultPath;
import org.springframework.beans.factory.annotation.Autowired;
import services.AddressService;
import services.CriteriaTranslator;
import services.TranslatorsCRUDService;

import java.util.ArrayList;
import java.util.List;

@Namespace("/Directories/Translators")
@ResultPath("/")
public class TranslatorsController extends ActionSupport implements ModelDriven {

    private TranslatorsCRUDService<Traductor> service;
    private List<Traductor> list = new ArrayList<Traductor>();
    private AddressService addressService;
    private Traductor translator = new Traductor();
    private List<Countries> countries = new ArrayList<Countries>();
    private List<Regions> regions = new ArrayList<Regions>();
    private List<Cities> cities = new ArrayList<Cities>();
    private Countries countrySelected;
    private Regions regionSelected;
    private Cities citySelected;
    private String editableTranslatorId;

    @Autowired
    public TranslatorsController(TranslatorsCRUDService<Traductor> translatorsCRUDService, AddressService addressService) {
        this.service = translatorsCRUDService;
        this.addressService = addressService;
        translator.setAddress(new Address());
        countries = this.addressService.getCountries();
    }

    @Action(value="main", results={
            @Result(name="success", location="/directories.translator.tiles", type="tiles")
    })
    public String execute() {
        list = new ArrayList<Traductor>(0);
        return SUCCESS;
    }

    @Action(value="list", results={
            @Result(name="success", location="/directories.translator.tiles", type="tiles")
    })
    public String list() {

        CriteriaTranslator cr = new CriteriaTranslator(translator);
        list = service.list(cr);

        return SUCCESS;
    }

    @Action(value="create", results={
            @Result(name="success",location="/directories.translator.create.tiles", type="tiles")
    })
    public String create() {
        if(editableTranslatorId != null){
            int id = Integer.parseInt(editableTranslatorId);
            translator = service.findById(id);

            countrySelected = addressService.getCountryByName(translator.getAddress().getCountry());

            regions = addressService.getRegions(countrySelected.getCountryId());
            regionSelected = addressService.getRegionByName(translator.getAddress().getProvince(), countrySelected.getCountryId().toString());

            cities = addressService.getCities(regionSelected.getRegionId());

            citySelected = addressService.getCityByName(translator.getAddress().getCity(), regionSelected.getRegionId().toString(), countrySelected.getCountryId().toString());

        }
        return SUCCESS;
    }

    @Action(value="save", results={
            @Result(name="success",location="main", type="redirectAction")
    })
    public String save() {
        addressService.checkCity(   translator.getAddress().getCity(),
                translator.getAddress().getProvince(),
                translator.getAddress().getCountry());

        service.save(translator);
        return SUCCESS;
    }

    @Action(value="delete", results={
            @Result(name="success",location="main", type="redirectAction")
    })
    public String delete() {
        service.delete(translator);
        return SUCCESS;
    }
    private String userName;

    @Action(value="getTranslatorsByName", results={@Result(name="success",location="/WEB-INF/jsp/Protocols/protocoldocuments/translatorsList.jsp")})
    public String getTranslatorsByName() {

        list = service.findName(userName);
        return SUCCESS;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    @Override
    public Object getModel() {
        return translator;
    }


    public List<Countries> getCountries() {
        return countries;
    }

    public void setCountries(List<Countries> countries) {
        this.countries = countries;
    }

    public List<Regions> getRegions() {
        return regions;
    }

    public void setRegions(List<Regions> regions) {
        this.regions = regions;
    }

    public List<Cities> getCities() {
        return cities;
    }

    public void setCities(List<Cities> cities) {
        this.cities = cities;
    }

    public Countries getCountrySelected() {
        return countrySelected;
    }

    public void setCountrySelected(Countries countrySelected) {
        this.countrySelected = countrySelected;
    }

    public Regions getRegionSelected() {
        return regionSelected;
    }

    public void setRegionSelected(Regions regionSelected) {
        this.regionSelected = regionSelected;
    }

    public Cities getCitySelected() {
        return citySelected;
    }

    public void setCitySelected(Cities citySelected) {
        this.citySelected = citySelected;
    }

    public Traductor getTranslator() {
        return translator;
    }

    public void setTranslator(Traductor translator) {
        this.translator = translator;
    }

    public List<Traductor> getList() {
        return list;
    }

    public void setList(List<Traductor> list) {
        this.list = list;
    }

    public String getEditableTranslatorId() {
        return editableTranslatorId;
    }

    public void setEditableTranslatorId(String editableTranslatorId) {
        this.editableTranslatorId = editableTranslatorId;
    }
}
